package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.common.article.dto.ArticleDto;
import com.heima.model.common.article.dto.ArticleHomeDto;
import com.heima.model.common.article.pojos.ApArticle;
import com.heima.model.common.article.pojos.ApArticleConfig;
import com.heima.model.common.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Override
    public ResponseResult selectApArticle(ArticleHomeDto dto, Short type) {
//        检验页码
        if(dto.getSize()==null||dto.getSize()==0){
            dto.setSize(10);
        }
//        校验频道id
        if(StringUtils.isBlank(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
//        校验时间
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
//        校验类型
        if(type!= ArticleConstants.LOADTYPE_LOAD_MORE &&type!=ArticleConstants.LOADTYPE_LOAD_NEW){
            type=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        List<ApArticle> apArticles = articleMapper.selectApArticle(dto, type);

        return ResponseResult.okResult(apArticles);
    }
    /**
     * 保存app端相关文章
     * @param dto
     * @return
     */
    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        if(org.springframework.util.StringUtils.isEmpty(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
      if(dto.getId()!=null){
          System.out.println(dto.getId());
//          修改文章
        articleMapper.updateById(dto);
          String content = dto.getContent();

//        修改文章内容
          ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers
                  .<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
         if(org.springframework.util.StringUtils.isEmpty(apArticleContent)){
             return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
         }
          apArticleContent.setContent(content);
          apArticleContentMapper.updateById(apArticleContent);
      }
      else {
//          保存文章
          articleMapper.insert(dto);
          //        保存文章配置
          Long atticle = dto.getId();
          ApArticleConfig apArticleConfig = new ApArticleConfig(atticle);
          apArticleConfigMapper.insert(apArticleConfig);
//          保存文章内容
          ApArticleContent apArticleContent = new ApArticleContent();
          apArticleContent.setArticleId(dto.getId());
          apArticleContent.setContent(dto.getContent());
          apArticleContentMapper.insert(apArticleContent);
      }
        //        异步调用生成文件到minio中
        articleFreemarkerService.buildArticleToMinIO(dto,dto.getContent());

        return ResponseResult.okResult(dto.getId());
    }
}
